/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CDU3.java
 *
 * Created on 06/10/2009, 11:12:12
 */

package tpgrandt.UI;

import tpgrandt.UI.model.ConsultaModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.TableColumn;
import tpgrandt.control.GestionConsultas;
import tpgrandt.control.GestionEquipo;
import tpgrandt.control.GestionPeriodoDeVeda;
import tpgrandt.dao.FechaDAO;
import tpgrandt.varios.Integrante;

/**
 *
 * @author Juan
 */
public class Consultas extends javax.swing.JInternalFrame {

    private GestionEquipo gestion;
    
    private GestionConsultas consultas;
    private String nombre;
    private String[] fechas;
    private Integrante[] plantel;
    private ConsultaModel titulares;
    private ConsultaModel suplentes;
    
    /** Creates new form CDU3 */
    public Consultas(GestionConsultas consultas, GestionEquipo gestion) {
        super("Mi equipo", false, true, false, false);
        this.consultas = consultas;
        initComponents();

        if(gestion == null)
            jButton2.setEnabled(false);
        this.gestion = gestion;

        nombre = consultas.getNombre();
        jFormattedTextField4.setText(nombre);
        int[] aux = consultas.listarFechas();
        if(aux[0] != FechaDAO.fechaActual()) {
            fechas = new String[aux.length + 1];
            fechas[0] = FechaDAO.fechaActual() + "";
            for(int i = 1; i < fechas.length; i++)
                fechas[i] = new Integer(aux[i-1]).toString();
        } else {
            fechas = new String[aux.length];
            for(int i = 0; i < aux.length; i++)
                fechas[i] = new Integer(aux[i]).toString();
        }
        jComboBox2.setModel(new DefaultComboBoxModel(fechas));

        int fecha = Integer.parseInt(fechas[0].toString().trim());
        plantel = consultas.getPlantel(fecha);
        jFormattedTextField2.setText(consultas.getPuntos(fecha) + "");
        jFormattedTextField3.setText(consultas.puntosTorneo().toString());

	if(GestionPeriodoDeVeda.esPeriodoDeVeda())
	    jButton2.setEnabled(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jFormattedTextField2 = new javax.swing.JFormattedTextField();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jFormattedTextField4 = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Equipo de fantasía:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Titulares:");

        Integrante tit[] = new Integrante[Integrante.TITULARES];
        for (int i = 0; i < Integrante.TITULARES; i++)
        tit[i] = consultas.ultimoPlantel()[i + 1];
        titulares = new ConsultaModel(tit, false);
        jTable1.setModel(titulares);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(jTable1);
        TableColumn tc = jTable1.getColumn("Posicion");
        tc.setPreferredWidth(45);
        tc = jTable1.getColumn("Apellido y nombres");
        tc.setPreferredWidth(180);
        tc = jTable1.getColumn("Club");
        tc.setPreferredWidth(180);
        tc = jTable1.getColumn("Puntaje");
        tc.setPreferredWidth(50);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Suplentes:");

        Integrante sup[] = new Integrante[Integrante.SUPLENTES];
        for (int i = Integrante.TITULARES; i < 15; i++)
        sup[i - Integrante.TITULARES] = consultas.ultimoPlantel()[i + 1];
        suplentes = new ConsultaModel(sup, false);
        jTable2.setModel(suplentes);
        jTable2.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane2.setViewportView(jTable2);
        tc = jTable2.getColumn("Posicion");
        tc.setPreferredWidth(45);
        tc = jTable2.getColumn("Apellido y nombres");
        tc.setPreferredWidth(180);
        tc = jTable2.getColumn("Club");
        tc.setPreferredWidth(180);
        tc = jTable2.getColumn("Puntaje");
        tc.setPreferredWidth(50);

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Modificar equipo");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel7.setText("Fecha:");

        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Puntos:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel5.setText("Puntos en el torneo:");

        jFormattedTextField2.setEditable(false);
        jFormattedTextField2.setText("< Datos pertinentes>");

        jFormattedTextField3.setEditable(false);
        jFormattedTextField3.setText("< Datos pertinentes>");

        jFormattedTextField4.setEditable(false);
        jFormattedTextField4.setText("< Datos pertinentes>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jFormattedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jFormattedTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(71, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        int fecha = Integer.parseInt(jComboBox2.getSelectedItem().toString().trim());
        plantel = consultas.getPlantel(fecha);

        Integrante lista[] = new Integrante[11];
        for (int i = 0; i < 11; i++)
            lista[i] = plantel[i + 1];

        if (fecha == FechaDAO.fechaActual() && GestionPeriodoDeVeda.esPeriodoDeVeda())
            titulares.cambiarLista(lista, false);
        else
            titulares.cambiarLista(lista, true);

        lista = new Integrante[4];
        for (int i = 11; i < 15; i++)
            lista[i - 11] = plantel[i + 1];

        if (fecha == FechaDAO.fechaActual() && GestionPeriodoDeVeda.esPeriodoDeVeda())
            suplentes.cambiarLista(lista, false);
        else
            suplentes.cambiarLista(lista, true);


        jTable1.updateUI();
        jTable2.updateUI();

        jFormattedTextField2.setText(consultas.getPuntos(fecha) + "");
}//GEN-LAST:event_jComboBox2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ModificacionEquipoDeFantasia cdu4 = new ModificacionEquipoDeFantasia(gestion);
        this.getParent().add(cdu4);
        cdu4.setVisible(true);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JFormattedTextField jFormattedTextField2;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JFormattedTextField jFormattedTextField4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    // End of variables declaration//GEN-END:variables

}
